/**
 * 网络请求配置
 */

import storage from "@/utils/storage";
import $utils from "@/utils/index";
const $store = require("@/store/index.js");
const $types = require("@/store/types.js");
const { $Toast, $Message } = require("../../static/iview/base/index");
var Fly = require("flyio/dist/npm/wx");
var fly = new Fly();
let host = "";
let linkUrl = ""
let imgUrl = "https://ks3-cn-beijing.ksyun.com/wxapp/wdrm/image/patient/";
if (process.env.NODE_ENV === "production") {
  // npm run build: ****
  host = process.env.http
  linkUrl = process.env.link
} else {
  // host = 'http://dev-ihosp.medicalmobile.cn' // 开发环境
  host = "https://test-ihosp.hiseemedical.com"; // 测试环境
  // host = 'https://release-ihosp.hiseemedical.com' // 预发/布环境
  // host = 'https://wdrm.hiseemedical.com' // 正式环境
  linkUrl = 'http://test-gkpt.medicalmobile.cn'
}
// 实例级配置
fly.config.baseURL = host + "/api"; // 测试环境地址
fly.config.timeout = 60000; // 超时时间
fly.config.method = "post"; // 请求方法
let isIpx = Boolean;
mpvue.getSystemInfo({
  success: function (res) {
    var model = res.model;
    if (model.search("iPhone X") != -1 || model.search("iPhone 11") != -1) {
      isIpx = true;
    } else {
      isIpx = false;
    }
    console.log(isIpx, "getSystemInfo");
    $store.default.commit($types.if_IPHONEX, isIpx);
  },
});

function request(url, data, options) {
  return fly.request(url, data, options);
}
//添加请求拦截器
fly.interceptors.request.use((request) => {
  if (request.showLoading) {
    mpvue.showLoading({
      title: "加载中",
      mask: true,
    });
  }
  let hiseeToken = storage.getToken() || "";
  //给所有请求添加自定义header
  request.headers["Authorization"] = "Bearer " + hiseeToken;
  request.headers["Hisee-Agent"] =
    "HISEE_PLATFROM=wchat,HISEE_VERSION=202002101,SOURCE=1,HISEE_TYPE=1";
  return request;
});

//添加响应拦截器，响应拦截器会在then/catch处理之前执行
fly.interceptors.response.use(
  (response) => {
    // console.log('拦截器：response', response)
    //只将请求结果的data字段返回
    // console.log('response.url',response.engine.responseURL)
    var url = response.engine.responseURL.split("api")[1];
    // console.log(url)
    if (response.data.code === 0) {
      if (response.request.showLoading == true) {
        mpvue.hideLoading();
      }

      return response.data;
    } else if (response.data.code === 110) {
      mpvue.redirectTo({
        url: "/pages/center/child/login/main",
      });
      $utils.showToast("text", "用户未登陆，请重新登录");
      storage.setToken(null);
      return err;
    } else if (
      response.data.code === 1 &&
      response.request.url !== "/user/query/phone"
    ) {
      return err;
    }
    if (
      request.url !== "/doctor/wechat/image/list" &&
      url !== "/appointment/payResult/get"
    ) {
      if (response.request.showLoading == true) {
        mpvue.hideLoading();
      }
    }
  },
  (err) => {
    mpvue.hideLoading();
    if (
      err.request.url === "/user/login" ||
      err.request.url === "/manage/live/login" ||
      err.request.url === "/live/update_status"
    ) {
      // 首页手机号码没注册
      console.log(err);
      $Toast({
        content: err.engine.response.msg,
      });
      return;
    }
    if (err.status === 0) {
      $utils.showToast("text", "网络连接异常,请稍后再试");
      return "网络连接异常";
    } else if (err.status === 1) {
      $utils.showToast("text", "网络连接异常,请稍后再试");
      return "网络连接超时";
    } else if (err.status === 401) {
      mpvue.redirectTo({
        url: "/pages/center/child/login/main",
      });
      return "用户未登录";
    } else if (err.engine.response.code && err.engine.response.code !== 0) {
      $utils.showToast("text", err.engine.response.msg);
      // $utils.showToast('text', err.engine.response.msg)
      return err.engine.response.msg;
    }
    $utils.showToast("text", "请求失败，请稍后再试");
    return "请求失败，请稍后再试";
    // return Promise.resolve('ssss')
  }
);

export default {
  request,
  host,
  imgUrl,
  linkUrl
}
